import db, {taskList} from '../../database/lowDB/connectDb.js';
import {unioformDateTransform,autoIncreamentKey} from '../../utils/index.js'

class taskListModel {
    constructor() {
        this.tableName = 'taskList';
    }

    // 获取用户的今日任务
    async getTodayTask(userInfo) {
        const {userId,roleCode,date,isUrgency} = userInfo;
        let todayTask =  taskList.filter((item)=>{
            return item.userId === userId && 
                    item.roleCode === roleCode && 
                    item.createDate.includes(date) && 
                    item.isUrgency === isUrgency
        })

        return todayTask.map(item => ({
             "id": item.id,
            "taskName": item.taskName,
            "startTime": item.startTime,
            "endTime": item.endTime,
            "isAlarmClock": item.isAlarmClock,
            "isUrgency": item.isUrgency,
        }))
    }


    
    // 新增任务  ——单个新增,只新增当日
    async addTask({userInfo,taskData}) {
        const {userId, roleCode} = userInfo;
        const {taskName, startTime,endTime,isAlarmClock, isUrgency} = taskData;
        const newTask = {
            id: autoIncreamentKey(taskList), 
            userId,
            roleCode,
            taskName,
            startTime,
            endTime,
            isUrgency,
            isAlarmClock,
            createDate: unioformDateTransform(new Date()),
        };
        taskList.unshift(newTask);
        db.write();
        return true;
    }
}   

export default new taskListModel();